<template>
  <div>
    <!-- 三方融资申请 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110"></TopFilter>
    <!--  -->
    <FilterTable
      ref="table"
      columnsRef="qjhFinanceApply"
      :isSelectColumn="false"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :onRowSelectChange="selectChangeHandle"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls">
        <el-button size="small" type="primary" @click="addHandle">新 增</el-button>
      </template>
    </FilterTable>

    <Drawer :visible.sync="visible" :title="drawerTitle" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <SonModel :type="handleType" :rowData="formData" @close="emitCloseHandler" />
    </Drawer>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { queryList, submitData } from '@/api/dealerFinance/FinanceManage/FinanceApply';

import SonModel from './SonModel';

export default {
  name: 'FinanceApply',
  mixins: [dictionary],
  data() {
    this.BaseTable = null;
    this.saveData = null;
    this.conf = { add: '新增', edit: '编辑', show: '明细' };

    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: queryList,
      fetchParams: {},
      formData: {},
      isCan: false,
      visible: false,
      handleType: '',
      drawerTitle: ''
    };
  },
  components: {
    SonModel
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {},
  methods: {
    addHandle() {
      this.handleType = 'add';
      this.formData = {};
      this.showHandler();
    },
    editHandle(row) {
      this.handleType = 'edit';
      this.formData = row;
      this.showHandler();
    },
    detailHandle(row) {
      this.handleType = 'show';
      this.formData = row;
      this.showHandler();
    },
    showHandler() {
      this.drawerTitle = `${this.conf[this.handleType]}`;
      this.visible = true;
    },
    emitCloseHandler(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
    selectChangeHandle(row) {
      console.log(row, '---选择的数据---');
      if (row.length > 0) {
        this.isCan = true;
        this.saveData = row[0];
      } else {
        this.isCan = false;
      }
    },
    changeHandle(params) {
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },

    // 提报
    async submitHandler() {
      if (this.saveData !== null) {
        let res = await submitData(this.saveData);
        if (res.resultCode === 200) {
          this.fetchParams = { ...this.fetchParams };
        }
      }
    },

    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '申请单号',
          fieldName: 'applyNo',
          placeholder: '请选择'
        },
        {
          type: 'SELECT',
          label: '审批状态',
          fieldName: 'businessStatus',
          placeholder: '请选择',
          itemList: [
            { text: '待提交', value: 60301001 },
            { text: '审批驳回', value: 60301006 }
          ]
        },
        {
          type: 'INPUT',
          label: '银行名称',
          fieldName: 'bankName',
          placeholder: '请选择'
        },
        {
          type: 'RANGE_DATE',
          label: '申请日期',
          fieldName: 'applyDateBegin|applyDateEnd',
          dateType: 'exactdaterange',
          placeholder: '请选择'
        },
        {
          type: 'SELECT',
          label: '申请类型',
          fieldName: 'applyType',
          placeholder: '请选择',
          itemList: this.createDictList('6073')
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row)}>
                  编辑
                </multiuse-button>
                <multiuse-button size="mini" type="text" onClick={() => this.detailHandle(props.row)}>
                  明细
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '状态',
          dataIndex: 'businessStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [
            { text: '待提交', value: 60301001 },
            { text: '审批驳回', value: 60301006 }
          ],
          render: props => {
            return <span>{this.createDictText(props.row.businessStatus, '6030')}</span>;
          }
        },
        {
          title: '申请单号',
          dataIndex: 'applyNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '银行名称',
          dataIndex: 'bankName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '申请类型',
          dataIndex: 'applyType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [{ text: '大客户融资', value: 60731002 }],
          render: props => {
            return <span>{this.createDictText(props.row.applyType, '6073')}</span>;
          }
        },
        {
          title: '出票金额（万）',
          dataIndex: 'billAmount',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true
        },
        {
          title: '出票期限',
          dataIndex: 'billDeadline', // TODO 字典6027
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6027'),
          render: props => {
            return <span>{this.createDictText(props.row.billDeadline, '6027')}</span>;
          }
        },
        {
          title: '银行联系人',
          dataIndex: 'linkMan',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: []
        },
        {
          title: '银行联系电话',
          dataIndex: 'linkPhone',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: []
        },

        {
          title: '申请日期',
          dataIndex: 'applyDate',
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '提车公司',
          dataIndex: 'salesCompany', // TODO 字典2555
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('2555'),
          render: props => {
            return <span>{this.createDictText(props.row.salesCompany, '2555')}</span>;
          }
        },
        {
          title: '收款方',
          dataIndex: 'payee',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6031'),
          render: props => {
            return <span>{this.createDictText(props.row.payee, '6031')}</span>;
          }
        },
        {
          title: '预提车型',
          dataIndex: 'reservedModel',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '备注',
          dataIndex: 'applyRemark',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    }
  }
};
</script>
